<?php
namespace controllers;
use controllers\api;
use Model\suppliers;
use SMB\Goods\product_class;
use SMB\Goods\supplier_goods_relation;
use SMB\Supplier\supplier_way;
use SMB\Stocks\product_stocks;
use Model\supplier_log;
use controllers\statistics;
use controllers\order;
class supinfo extends api{
    //登录后供货商信息
    public function info(){
        //获取供货商信息表
        $db = new suppliers();
        $supplier_info = $db->get_sup_info();
        //送货方式
        $supplier_db = new supplier_way();
        $send_way_arr = $supplier_db->get_way('send_way');
        //结算方式
        $payment_way_arr = $supplier_db->get_way('payment_way');
        $data = array();
            $data['supplier_name'] = $supplier_info['supplier_name']; //供应商名称
            $data['address'] = $supplier_info['receiver']['proviceFirstStageName'].$supplier_info['receiver']['addressCitySecondStageName'].$supplier_info['receiver']['addressCountiesThirdStageName'].$supplier_info['receiver']['addressDetailInfo']; //供应商详细地址
            $data['send_way'] = @empty($send_way_arr[$supplier_info['send_way']])?'无':$send_way_arr[$supplier_info['send_way']];//送货方式
            $data['payment_way'] = @empty($payment_way_arr[$supplier_info['checkout_method']])?'无':$payment_way_arr[$supplier_info['checkout_method']];//结算方式
            $data['cost_way'] = @empty($supplier_info['send_conditions']) ? '无' : @$supplier_info['send_conditions'];//起送条件

        //昨日商品销售量
        $statistics_model = new statistics();
        $sale_arr = $statistics_model->sale();
        $data['sale_count'] = $sale_arr['sale_count'];
        //昨日零库存商品个数
        $stocks_arr = $statistics_model->zero_stock();
        $data['num'] = $stocks_arr['zero_stocks_count'];
        //订单情况
        $order_model = new order();
        $order_num_arr = $order_model->number();
        $data['nums'] = $order_num_arr;
        $this->success($data);
    }
    //供应商详细信息
    public function supplier_info(){
        //供货商信息表
        $db = new suppliers();
        //根据登陆后账号查询供货商信息;
        $db = new suppliers();
        $supplier_info = $db->get_sup_info();
        $model_product_class=new product_class();//类目
        $class_cur = $model_product_class->find();
        $product_class = array();
        foreach ($class_cur as $item) {
            $product_class[$item['_id'].''] = $item['name'];
        }
        $supplier_db = new supplier_way();
        //送货方式
        $send_way_arr = $supplier_db->get_way('send_way');
        //结算方式
        $payment_way_arr = $supplier_db->get_way('payment_way');
        $data = array();
        //重组数组
            $data['supplier_name'] = $supplier_info['supplier_name']; //供应商名称
            $data['address'] = $supplier_info['receiver']['proviceFirstStageName'].$supplier_info['receiver']['addressCitySecondStageName'].$supplier_info['receiver']['addressCountiesThirdStageName'].$supplier_info['receiver']['addressDetailInfo']; //供应商详细地址
            $data['Category'] = array($product_class[$supplier_info['supplier_category']]);//供应商类目
            $method = '';//合作方式;
            $send_way = @empty($send_way_arr[$supplier_info['send_way']])?'无':$send_way_arr[$supplier_info['send_way']];//送货方式
            $payment_way= @empty($payment_way_arr[$supplier_info['checkout_method']])?'无':$payment_way_arr[$supplier_info['checkout_method']];//结算方式
            $cost_way = @empty($supplier_info['send_conditions']) ? '0' : @$supplier_info['send_conditions'];//起送条件
            $create_time = @empty($supplier_info['create_time']) ? '0' : @date('Y-m-d',$supplier_info['create_time']);//创建时间
            $linkman_nam = @empty($supplier_info['receiver']['userName'])?'':@$supplier_info['receiver']['userName'];//供应商联系人
            $linkman_phone = @empty($supplier_info['receiver']['userName'])?'':@$supplier_info['receiver']['telNumber'];//联系号码
            $linkman_position = @empty($supplier_info['receiver']['position'])?'':@$supplier_info['receiver']['position'];//职位
            $data['cooperation'] = array($method,$payment_way,$send_way,$cost_way,$create_time);
            $data['account'] = array();
            $data['contact'][] = array('name'=>$linkman_nam,'position'=>$linkman_position,'tel'=>$linkman_phone,'id'=>1);
            if(isset($supplier_info['receiver']['linkman'])){
                foreach ($supplier_info['receiver']['linkman'] as $v){
                    $data['contact'][] = array('name'=>$v['name'],'position'=>$v['position'],'tel'=>$v['tel'],'id'=>$v['id']);
                }
            }


        $this->success($data);
    }
    //添加供应商联系人
    public function add_linkman(){
        $name = $_REQUEST['name'];
        $tel = $_REQUEST['tel'];
        $position = $_REQUEST['position'];
        $supplier_db = new suppliers();
        $supplier_info = $supplier_db->get_sup_info();

        if(isset($supplier_info['receiver']['linkman'])){
            $num = count($supplier_info['receiver']['linkman']);
            $id = ($num+2);
        }else{
            $id = 2;
        }
        $res = $supplier_db->update(array('_id'=>$supplier_info['_id']),array('$push'=>array('receiver.linkman'=>array('name'=>$name,'tel'=>$tel,'position'=>$position,'id'=>$id))));
        if($res['ok']){
            $supplier_log_model = new supplier_log();
            $result = $supplier_log_model->add($supplier_info['supplier_name'],$supplier_info['supplier_id'],'添加了新的联系人','');
            if($result){
                $this->success(array('name'=>$name,'tel'=>$tel,'position'=>$position,'id'=>$id));
            }else{
                $this->error('操作失败');
            }
        }else{
            $this->error('操作失败');
        }
    }
    //修改供应商联系人
    public function update_linkman()
    {
        $name = $_REQUEST['name'];
        $tel = $_REQUEST['tel'];
        $position = $_REQUEST['position'];
        $id = $_REQUEST['id'];
        $k = $id-2;
        $supplier_db = new suppliers();
        $supplier_info = $supplier_db->get_sup_info();
        $res = $supplier_db->update(array('_id'=>$supplier_info['_id']),array('$set'=>array('receiver.linkman.'.$k=>array('name'=>$name,'tel'=>$tel,'position'=>$position,'id'=>$id))));
        if($res['ok']){
            $supplier_log_model = new supplier_log();
            $result = $supplier_log_model->add($supplier_info['supplier_name'],$supplier_info['_id'],'修改了联系人信息','');
            if($result){
                $this->success(array('name'=>$name,'tel'=>$tel,'position'=>$position,'id'=>$id));
            }else{
                $this->error('操作失败');
            }
        }else{
            $this->error('操作失误');
        }
    }


}

